<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
function cms_modelmodel_select() {
    $db = xt_load('db','icweb');
    $models = array();
    $result = $db->query("SELECT * FROM `".xt_tname('model','icweb')."`");
    while($r = $db->fetch_array($result)) {
        $models[] = $r;
    }
    $db->free_result($result);
    return $models;
}
function cms_modelmodel_get($modelid) {
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname('model','icweb')."` WHERE `modelid`=$modelid");
    if(!$data) return FALSE;
    return $data;
}
function cms_modelmodel_disable($modelid) {
    $db = xt_load('db','icweb');
    return $db->query("UPDATE `".xt_tname('model','icweb')."` SET `disabled` = 1-`disabled` WHERE `modelid`=$modelid");
}
function cms_modelmodel_add($model) {
    $db = xt_load('db','icweb');
    
    if($db->table_status('c_'.$model['tablename'],'icweb')) return -2;
    $name = addslashes($model['name']);
    if($db->result($db->query("SELECT count(*) from `".xt_tname('model','icweb')."` WHERE `name`='$name'"))>0)
        return -3;
    $modelid = $db->insert('model', $model,1,'icweb');    
    $arr_search = array('$tablename', '$table_model_field', '$modelid');
	$arr_replace = array(xt_tname('c_'.$model['tablename'],'icweb'), xt_tname('model_field','icweb'), $modelid);
	$sql = xt_readfile(MS_APPPATH.'fields/model.sql');
	$sql = str_replace($arr_search, $arr_replace, $sql);
	$dm = xt_load('model','databasemodel');
	$sqls = $dm('splitsql',$sql);
	foreach($sqls as $s) {
	    if(trim($s)!='') $db->query($s);   
	}

	cms_modelmodel_cache();
	$m = xt_load('model','cms.fieldmodel');
    $m('cache',$modelid);
	return $modelid;
}
function cms_modelmodel_edit($modelid, $model) {
    $db = xt_load('db','icweb');
    $name = addslashes($model['name']);
    if($db->result($db->query("SELECT count(*) from `".xt_tname('model','icweb')."` WHERE `name`='$name' AND modelid <> $modelid"))>0)
        return -3;
    $r = $db->update('model', $model,"modelid=$modelid",'icweb');
    /*cms_modelmodel_cache();
	$m = xt_load('model','cms.fieldmodel');
    $m('cache',$modelid);*/
    return $r;        
}
function cms_modelmodel_cache() {
    @set_time_limit(600);
    $fields = array();
    $files = glob(MS_APPPATH.'fields/*');
    foreach($files as $file) {
        if(!is_dir($file)) continue;
        $fields[] = basename($file);
    }
    cms_modelmodel_cache_class($fields, 'form');
    cms_modelmodel_cache_class($fields, 'input');
    cms_modelmodel_cache_class($fields, 'output');

     
    /*cms_modelmodel_cache_class($fields, 'search');cms_modelmodel_cache_class($fields, 'update');
    cms_modelmodel_cache_class($fields, 'search_form');
    cms_modelmodel_cache_class($fields, 'tag');
    cms_modelmodel_cache_class($fields, 'tag_form');*/
    return true;
}
function cms_modelmodel_cache_class($fields, $classname) {
    $data = '';
    foreach($fields as $field) {
        $r = xt_readfile(MS_APPPATH.'fields/'.$field.'/'.$classname.'.inc.php');
        if($r) $data .= $r;
    }
    $classcode = xt_readfile(MS_APPPATH.'fields/content_'.$classname.'.class.php');
    if(!$classcode) return false;
    $data = str_replace('}?>', $data."}\r\n?>", $classcode);
    return xt_writefile(MS_CACHEPATH.'model/content_'.$classname.'.cache.php', $data,'text');
}
?>